

from selenium.webdriver.support.wait import WebDriverWait
from Common.Utils import blog_driver
from Common.FormHelper import FormHelper
from LoginTest import Login



class HomePage:
    url=""
    driver=""

    def __init__(self):
        self.url="http://49.235.61.184:8080/home"
        self.driver=blog_driver.driver
        self._auto_login()
        self.driver.get(self.url)
        self.form = FormHelper(self.driver)


    def _auto_login(self):
        # 先尝试访问首页，判断是否已登录（未登录会跳转到登录页）
        self.driver.get(self.url)
        WebDriverWait(self.driver,10).until(
            lambda d:"login" in d.current_url or "home" in d.current_url
        )

        if "login" in self.driver.current_url:
            login=Login()
            login.LoginSuTest()
    #检测页面元素
    def pageElements(self):
        elememts_check={
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > div > span",
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.avatar___3ZBop > span > img",
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-head > div > div",
            "#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div.content___1eLtZ > div.contentTitle___Yu-cR"
        }
        blog_driver.GetScreeShot()
        if self.form.are_all_elements_present(elememts_check,5):
            print("元素都存在")
        else:
            print("元素存在有问题")

    #检测创建文件，试卷是否成功
    def CreatSucTest(self):
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(1)")
        # 统一等待后截图，增加额外延迟确保页面完全加载
        self.form.screenshot_after_presence("#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p", 15, 3)
        self.driver.back()
        self.form.click("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-col.ant-col-xs-24.ant-col-sm-24.ant-col-md-24.ant-col-lg-24.ant-col-xl-8 > div > div.ant-card-body > div > a:nth-child(2)")
        # 统一等待后截图，增加额外延迟确保页面完全加载
        self.form.screenshot_after_presence("#editorContent > div > div > div.header > div.header-content > div.sc-aXZVg.LyTMt.title.quill-container.not-focus > div > pre > div.ql-editor > p", 15, 3)
        self.driver.back()

    #检测，我的考试，我的试卷，问卷记录，考试记录按钮
    def CheckSucTest(self):
        #我的考试
        self.form.click("#rc-tabs-0-tab-exam")
        self.form.screenshot_after_presence("#rc-tabs-0-panel-exam > div > div > div > div.ant-table-wrapper > div > div > div > div > div > table > thead > tr > th:nth-child(1)", 15, 3)
        #我的问卷
        self.form.click("#rc-tabs-0-tab-survey")
        self.form.screenshot_after_presence("#rc-tabs-0-panel-survey > div > div > div > div.ant-table-wrapper > div > div > div > div > div > table > thead > tr > th:nth-child(1)", 15, 3)
        #问卷记录
        self.form.click("#rc-tabs-0-tab-surveyHistory")
        self.form.screenshot_after_presence("#rc-tabs-0-panel-surveyHistory > div > div > div > div.ant-table-wrapper > div > div > div > div > div > table > thead > tr > th:nth-child(1)", 15, 3)
        #考试记录
        self.form.click("#rc-tabs-0-tab-examHistory")
        self.form.screenshot_after_presence("#rc-tabs-0-panel-examHistory > div > div > div > div.ant-table-wrapper > div > div > div > div > div > table > thead > tr > th:nth-child(1)", 15, 3)





# home=HomePage()
# home.CheckSucTest()